package com.cms.cmsbackend.controller;

import com.cms.cmsbackend.pojo.Link;
import com.cms.cmsbackend.service.LinkService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags = "链接管理")
@RequestMapping("/link")
public class LinkController {
    @Autowired
    private LinkService linkService;
    @PostMapping("/add")
    @ApiOperation("链接新增")
    public String add(Link link){
        linkService.save(link);
        return "新增数据成功";
    }

    @GetMapping("/selectByPage")
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name="size",value = "页面大小",required = true),
            @ApiImplicitParam(name="page",value = "页码",required = true)
    })
    public PageInfo selectByPage(int size,int page){

        PageInfo pageInfo=linkService.findByPage(size,page);
        return pageInfo;
    }

    @GetMapping("/delete")
    @ApiOperation(value = "链接删除")
    @ApiImplicitParam(name = "id",value = "删除链接编号",required = true)
    public String del(int id){
        linkService.delete(id);
        return "删除成功";
    }

    /*
    当修改数据的时候需要将这个数据原来的信息展示
    当展示完了以后用户可进行修改
    其他不修改的数据就不变
     */
    @GetMapping("/selsectById")
    @ApiOperation("根据id查询链接")
    public Link selectById(int id){
        Link link=linkService.findById(id);
        return link;
    }
    @PostMapping("/updata")
    @ApiOperation(value = "链接更新")
    public String updata(Link link){
        linkService.updatalink(link);
        return "更新成功";
    }

    /*
    在一些网站存在批量删除
    用户勾选了几个选项，然后点击删除
    勾选的就全部删除了
    删除单个选项是 根据id 删除
     */

    @GetMapping("/batchDelete")
    @ApiOperation("批量删除")
    @ApiImplicitParam(name="ids",value = "批量id，写法如下：1,2,3,4",required = true)
    public String batchDelete(int[] ids){
        linkService.batchdel(ids);
        return "操作成功";
    }
}
